#!/bin/bash

CONFIG_FILE="./config/.config"

if [ "$1" == "lunch" ]; then
	
	src_list=($(ls config/))
	echo
	echo "You're switch mk file"
	echo ""

	echo ${src_list[@]} | xargs -n 1 | sed "=" | sed "N;s/\n/. /"

	read -p "Which would you like? [0]: " INDEX
	INDEX=$((${INDEX:-0} - 1))

	if echo $INDEX | grep -vq [^0-9]; then
		mysel="${src_list[$INDEX]}"
	else
		echo "Not such file or dir..."
		exit -1
	fi
	
	ln -sf $mysel $CONFIG_FILE

elif [ "$1" == "all" ]; then
	[ ! -L "$CONFIG_FILE" ] && echo "No such mk link file, Please execute \"./build.sh lunch\" to switch mk file" && exit -1
	source $CONFIG_FILE
	rm -rf output
	mkdir -p output/extract/opt/factory_test/scripts/
	for i in $TEST_PROJ
	do
		cp scripts/$i output/extract/opt/factory_test/scripts/ -rfp
	done

	rm -rf build
	mkdir build
	cd build
	cmake ../
	make
	cp demo ../ -rfp
	cd - > /dev/null
	cp build/demo output/extract/opt/factory_test/ -rfp
	cp config output/extract/opt/factory_test/config -rfp
	cp DEBIAN output/extract/DEBIAN -rfp
	cp run.sh output/extract/opt/factory_test -rfp
	cp scripts_lib output/extract/opt/factory_test -rfp
	cp data output/extract/opt/factory_test -rfp
	cp tools output/extract/opt/factory_test -rfp
	
	cd output
	dpkg-deb -b extract ./
	cd - > /dev/null
else
	[ ! -L "$CONFIG_FILE" ] && echo "No such mk link file, Please execute \"./build.sh lunch\" to switch mk file" && exit -1
	source $CONFIG_FILE
	rm -rf output
	mkdir -p output/extract/opt/factory_test/scripts/
	for i in $TEST_PROJ
	do
		cp scripts/$i output/extract/opt/factory_test/scripts/ -rfp
	done

	cp demo output/extract/opt/factory_test/ -rfp
	cp config output/extract/opt/factory_test/config -rfp
	cp DEBIAN output/extract/DEBIAN -rfp
	cp run.sh output/extract/opt/factory_test -rfp
	cp scripts_lib output/extract/opt/factory_test -rfp
	cp data output/extract/opt/factory_test -rfp
	cp tools output/extract/opt/factory_test -rfp
	
	cd output
	dpkg-deb -b extract ./
	cd - > /dev/null

fi


